// pages/componet/auditUserDetail/auditUserDetail.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user: {
      id: 1,
      name: '貂蝉待审',
      mobile: 1596965316,
      category: 'women'
    },
    index: 0,
    selectedMajor: 1,
    roleIndex: 0,
    status: '待审核',
    marjor_array: [
      { id: 1, category_name: '建筑' },
      { id: 2, category_name: '机电' },
      { id: 3, category_name: '土木' },
      { id: 4, category_name: '机械' }
    ],
    role_array: [
      { id: 0, role_name: '用户' },
      { id: 1, role_name: '合伙人' },
      { id: 2, role_name: '管理员' }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  //
  //  let _that = this;
  //  app.getToken().then(function (){
  //    _that.loadData();
  //  });
  //},
  //
  //loadData: function (){
    var thz = this;
    wx.request({
      url: app.globalData.requestUrl + 'api/major/lists',
      method: 'POST',
      data: {},
      success: function (ret) {
        var data = ret.data;
        thz.setData(
          {
            major_array: data.data.lists
          }
        );
      },
      fail: function (e) {
        wx.showLoading({
          title: '系统错误请稍后重试',
        });
      }
    });

    // end load majors
    wx.request({
      url: app.globalData.requestUrl + 'api/user/detail',
      method: 'POST',
      data: {
        id: options.id,
      },
      success: function (ret) {
        wx.hideLoading();
        let data = ret.data;
        let _status = data.data.user.status;
        thz.setData(
          {
            user: data.data.user,
            selectedMajor: data.data.user.major,
            roleIndex: data.data.user.role
          }
        );

        //根据major设置Index值
        let major1 = thz.data.user.major;
        let major_arr1 = thz.data.major_array;
        if (major1 = null || major1 == '') return false;
        if (major_arr1 == null || major_arr1.length == 0) return false;
        for (var i in major_arr1) {
          if (thz.data.user.major == major_arr1[i].id) {
            thz.setData({
              index: i
            });
          }
        }

      },
      fail: function (e) {
        wx.showLoading({
          title: '系统错误请稍后重试',
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  pass: function ()
  {
    let thz = this;
    let uid = thz.data.user.id;
    let ty = 1; //通过
    let role = thz.data.roleIndex;
    //let major = thz.data.selectedMajor;
    wx.request({
      url: app.globalData.requestUrl + 'api/user/check',
      method: 'POST',
      data: {
        id: uid,
        'type': ty,
        role: role
      },
      success: function (ret) {
        var data = ret.data;
        thz.setData(
          {
            status: '已通过'
          }
        );
        //成功提示
        wx.showToast({
          title: '操作成功',
          icon: 'success',
          duration: 2000
        });
        thz.redirectURL();
      },
      fail: function (e) {
        wx.showLoading({
          title: '系统错误请稍后重试',
        });
      }
    })
  },
  refuse: function ()
  {
    let thz = this;
    let uid = thz.data.user.id;
    let ty = 2; //拒绝
    let role = thz.data.user.roleIndex;
    wx.request({
      url: app.globalData.requestUrl + 'api/user/check',
      method: 'POST',
      data: {
        id: uid,
        'type': ty,
        role: role
      },
      success: function (ret) {
        let data = ret.data;
        thz.setData(
          {
            status: '未通过'
          }
        );
        //成功提示
        wx.showToast({
          title: '操作成功',
          icon: 'success',
          duration: 2000
        });

        thz.redirectURL();
      },
      fail: function (e) {
        wx.showLoading({
          title: '系统错误请稍后重试',
        });
      }
    })
  },
  redirectURL: function ()
  {
    wx.navigateBack({
      delta: 1
    })
  },
  bindPickerChange: function (e) {
    let _index = e.detail.value;
    let majorList = this.data.major_array;
    let major;
    if (majorList != null && majorList.length > 0) {
      major = majorList[_index].id;
    } else {
      major = 1;
    }
   
    this.setData(
      {
        index: _index,
        selectedMajor: major
      }
    );
  },
  bindRolePickerChange: function (e)
  {
    let _index = e.detail.value;

    this.setData(
      {
        roleIndex: _index
      }
    );
  }

})